package com.braveman.im.ui.weiget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.braveman.im.R;


public class ButtonItem extends LinearLayout {
    //基本设置消息
    public TextView tv_title; //左边的标题
    public TextView tv_title_del; //左边的详情
    public TextView tv_des;  //右边的详情
    public ImageView iv_title; //左边的图片
    public ImageView iv_des; //详情的图片
    public ImageView iv_check; //选择的图片
    public ImageView iv_click; //点击的图片


    //处理红点用的
    public RelativeLayout rl_iv_title;
    public RelativeLayout rl_tv_title;
    public RelativeLayout rl_iv_des;
    public RelativeLayout rl_iv_click;


    private Context context;
    private String title = "设置";
    private String title_del = "";
    private String des;
    private Bitmap title_image;
    private Bitmap des_image;
    private Bitmap click_image;
    private Bitmap check_image;

    private int childViewCount; //线性布局的子View的个数
    private int childViewCountID = 1000;

    private float buttonitem_title_size = 15;
    private float buttonitem_des_size = 14;

    //构造方法
    public ButtonItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ButtonItem);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);

            if (attr == R.styleable.ButtonItem_title_image) {
                BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
                if (null != drawable)
                    title_image = drawable.getBitmap();
            } else if (attr == R.styleable.ButtonItem_titleText) {
                title = a.getString(attr);
            } else if (attr == R.styleable.ButtonItem_titleTextDel) {
                title_del = a.getString(attr);
            } else if (attr == R.styleable.ButtonItem_des_image) {
                BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
                if (null != drawable)
                    des_image = drawable.getBitmap();
            } else if (attr == R.styleable.ButtonItem_des) {
                des = a.getString(attr);
            } else if (attr == R.styleable.ButtonItem_check_image) {
                BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
                if (null != drawable)
                    check_image = drawable.getBitmap();
            } else if (attr == R.styleable.ButtonItem_click_image) {
                BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
                if (null != drawable)
                    click_image = drawable.getBitmap();
            }
        }

        //初始化布局
        initView();

    }

    /**
     * 添加自定义View
     *
     * @param view 自定义的View
     * @return 返回所在LinearLayout的下标位置
     */
    public int AddButtonView(View view) {
        int index = childViewCount++;
        addView(view, index);
        return index;
    }


    private void initView() {
        //title_image
        if (null != title_image) {
            iv_title = new ImageView(context);
            iv_title.setId(childViewCountID++);
            iv_title.setFocusable(false);
            iv_title.setImageBitmap(title_image);
            rl_iv_title = new RelativeLayout(context);
            rl_iv_title.addView(iv_title);
            addView(rl_iv_title, childViewCount++);//添加title_image
        }
        //title
        if (null != title) {
            tv_title = new TextView(context);
            tv_title.setId(childViewCountID++);
            tv_title.setTextSize(buttonitem_title_size);
            tv_title.setFocusable(false);
            tv_title.setText(title);
            tv_title.setTextColor(getResources().getColor(R.color.color_01));
            rl_tv_title = new RelativeLayout(context);
            rl_tv_title.addView(tv_title);
            LayoutParams parm = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
            addView(rl_tv_title, childViewCount++, parm);//添加title
        }

        if (null != title_del) {
            tv_title_del = new TextView(context);
            tv_title_del.setId(childViewCountID++);
            tv_title_del.setTextSize(buttonitem_des_size);
            tv_title_del.setFocusable(false);
            tv_title_del.setText(title_del);
            tv_title_del.setTextColor(getResources().getColor(R.color.color_10));
            tv_title_del.setSingleLine(true);
            tv_title_del.setEllipsize(TextUtils.TruncateAt.END);
            rl_tv_title = new RelativeLayout(context);
            rl_tv_title.addView(tv_title_del);
            LayoutParams parm = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
            addView(rl_tv_title, childViewCount++, parm);//添加title详情
        }
        //des_image
        if (null != des_image) {
            iv_des = new ImageView(context);
            iv_des.setId(childViewCountID++);
            iv_des.setFocusable(false);
            iv_des.setImageBitmap(des_image);
            rl_iv_des = new RelativeLayout(context);
            rl_iv_des.addView(iv_des);
            addView(rl_iv_des, childViewCount++);//添加title_image
        }
        //des
        if (null != des) {
            tv_des = new TextView(context);
            tv_des.setTextSize(buttonitem_des_size);
            tv_des.setFocusable(false);
            tv_des.setText(des);
            tv_des.setPadding(8, 0, 8, 0);
            addView(tv_des, childViewCount++);//添加title
        }
        //check
        if (null != check_image) {
            iv_check = new ImageView(context);
            iv_check.setFocusable(false);
            iv_check.setImageBitmap(check_image);
            addView(iv_check, childViewCount++);//添加title
        }
        //click
        if (null != click_image) {
            iv_click = new ImageView(context);
            iv_click.setId(childViewCountID++);
            iv_click.setFocusable(false);
            iv_click.setImageBitmap(click_image);
            rl_iv_click = new RelativeLayout(context);
            rl_iv_click.addView(iv_click);
            addView(rl_iv_click, childViewCount++);//添加title
        }


    }

}

	
	
	
	
